package review.system.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


import review.dao.DepartmentDAO;
import review.entity.Department;

@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {
	
	@Autowired
	private DepartmentDAO departmentDAO;
	
	public void addDepartment(Department dept) {
		if( !StringUtils.hasText(dept.getName()) ) {
			throw new RuntimeException("部门名称不能为空");
		}
		Department deptExample = new Department();
		deptExample.setName(dept.getName());
		deptExample.setParent(dept.getParent());
		Example<Department> example = Example.of(deptExample);
		if (departmentDAO.exists(example)) {
			throw new RuntimeException("部门名称不能重复");
		}
		departmentDAO.save(dept);
		dept.setOrderindex(dept.getId());
	}
	
	public void updateDepartment(Department dept) {
		if( !StringUtils.hasText(dept.getName()) ) {
			throw new RuntimeException("部门名称不能为空");
		}
		if (dept.getId() == null) {
			throw new RuntimeException("修改的部门ID为空");
		}
		Department deptExample = new Department();
		deptExample.setName(dept.getName());
		deptExample.setParent(dept.getParent());
		Example<Department> example = Example.of(deptExample);
		List<Department> list = departmentDAO.findAll(example);
		for (Department department : list) {
			if (department.getId().intValue() != dept.getId()) {
				throw new RuntimeException("部门名称重复");
			}
		}
		departmentDAO.saveAndFlush(dept);
	}
	
	public void deleteDepartment(Department dept) {
		if (dept.getId() == null) {
			throw new RuntimeException("部门ID为空");
		}
		Department deptExample = new Department();		
		deptExample.setParent(dept.getId());
		Example<Department> example = Example.of(deptExample);
		List<Department> list = departmentDAO.findAll(example);
		for (Department department : list) {
			deleteDepartment(department);
		}
		departmentDAO.deleteById(dept.getId());
	}
	
	public Page<Department> findAll(int page, int size, Department dept){
		ExampleMatcher matcher = ExampleMatcher.matching()
				.withIgnoreNullValues()
				.withMatcher("name", GenericPropertyMatchers.contains());
		Example<Department> example = Example.of(dept, matcher);
		Pageable pageable = PageRequest.of(page -1 , size, Sort.by(Direction.ASC, "companyid","parent","orderindex"));
		return departmentDAO.findAll(example,pageable);
	}
	
	public List<Department> findAllByCompany(Integer companyId){
		if (companyId == null) {
			return departmentDAO.findAll();
		}
		Department department = new Department();
		department.setCompanyid(companyId);
		Example<Department> deptExample = Example.of(department);
		return departmentDAO.findAll(deptExample,Sort.by(Direction.ASC, "parent","orderindex"));
	}

}
